package com.lst.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lst.api.entity.AssetsCarInfo;
import com.lst.api.mapper.AssetsCarInfoMapper;
import com.lst.api.service.IAssetsCarInfoService;
import com.lst.api.vo.AssetsCarInfoVo;
import com.lst.tools.mybatis.service.impl.BaseService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 资产信息中的车辆信息业务层实现
 *
 * @author Frank
 * @date 2019-12-30 10:31:37
 * @version: V1.0
 */
@Service
public class AssetsCarInfoService extends BaseService<AssetsCarInfoMapper, AssetsCarInfo> implements IAssetsCarInfoService {

    @Override
    public boolean saveOrUpdate(long userId, String applyNo, AssetsCarInfoVo assetsCarInfoVo) {
        if (assetsCarInfoVo == null) {
            return false;
        }
        assetsCarInfoVo.check();
        AssetsCarInfo assetsCarInfo = new AssetsCarInfo(userId, applyNo, assetsCarInfoVo);
        QueryWrapper<AssetsCarInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(AssetsCarInfo.APPLY_NO, applyNo)
                .eq(AssetsCarInfo.PROMO_USER_ID, userId);
        int ret = super.saveNotExist(assetsCarInfo, queryWrapper);
        if (ret == 0) {
            UpdateWrapper<AssetsCarInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper
                    .eq(AssetsCarInfo.APPLY_NO, applyNo)
                    .eq(AssetsCarInfo.PROMO_USER_ID, userId)
                    .set(AssetsCarInfo.UPDATE_AT, new Date())
                    .set(AssetsCarInfo.CAR_BRAND_ID, assetsCarInfo.getCarBrandId())
                    .set(AssetsCarInfo.CAR_BUY_AT, assetsCarInfo.getCarBuyAt())
                    .set(AssetsCarInfo.CAR_BUY_TOTAL_AMOUNT, assetsCarInfo.getCarBuyTotalAmount())
                    .set(AssetsCarInfo.CAR_BUY_TYPE, assetsCarInfo.getCarBuyType())
                    .set(AssetsCarInfo.INSURANCE_COMPANY, assetsCarInfo.getInsuranceCompany())
                    .set(AssetsCarInfo.INSURANCE_PAY_WAY, assetsCarInfo.getInsurancePayWay())
                    .set(AssetsCarInfo.INSURANCE_PAY_AMOUNT, assetsCarInfo.getInsurancePayAmount());
            return super.update(updateWrapper);
        }
        return true;
    }

    @Override
    public AssetsCarInfo getByApplyNo(String applyNo) {
        QueryWrapper<AssetsCarInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(AssetsCarInfo.APPLY_NO, applyNo);
        return super.getOne(queryWrapper);
    }
}